package com.yoomiito.app.adapter.my;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.widget.NumControlView;
import com.yoomiito.app.widget.OnNumberChangeListener;
import com.yoomiito.app.widget.SelectView;

public class ShopCarAdapter extends BaseQuickAdapter<GoodsDetail, BaseViewHolder> {


    OnClick onClick;

    public ShopCarAdapter() {
        super(R.layout.item_shop_car);
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsDetail item) {
        ImageView roundImageView = helper.getView(R.id.item_order_goods_iv);
        ImageLoader.getInstance().into(roundImageView.getContext(), item.banner, roundImageView);
        helper.setText(R.id.item_order_goods_title, item.getTitle());
        helper.setText(R.id.item_order_goods_title1, item.getProductPoint());
        SelectView sv = helper.getView(R.id.selectView);
        helper.setText(R.id.item_order_goods_price, "¥" + item.getSalePrice());
        TextView tvSend = helper.getView(R.id.item_order_goods_price2);
        helper.addOnClickListener(R.id.item_order_goods_title);
        NumControlView numControlView = helper.getView(R.id.num_control_view);
        numControlView.setStep(item.againStep);
        numControlView.setMax(item.maxNum);
        numControlView.setNum(item.againNum);
        item.setNum = item.againNum;
        numControlView.setCar();

        if(item.giveStart >0){
            tvSend.setText("" );
            tvSend.setVisibility(View.VISIBLE);
        }else {
            tvSend.setVisibility(View.GONE);
        }
        sv.setChangeListener((view, isSelected) -> {
            item.setSelected(isSelected);
            if (onClick != null) {
                onClick.onGetGood("",-10000,item.productId,helper.getLayoutPosition());
            }
        });
        numControlView.addOnInputNumChangeListener(num -> {
            onClick.onGetGood("",num,item.productId,helper.getLayoutPosition());
                });
        numControlView.setListener(new OnNumberChangeListener() {
            @Override
            public void numberChange(int num) {
                item.setNum = num;

                if(num > item.giveStart){
                    if(item.multiple!=0){
                        int num1 = num / item.multiple;
                        tvSend.setText("赠送数量:"+ num1+"");
                    }
                }else {
                    tvSend.setText("" );
                }
            }
        });
//        if (item.goodsNum == 0) {
//            numControlView.setNoClick(false);
//        } else {
//            numControlView.setNoClick(true);
//        }
        numControlView.setText("购买数量不能小与0", "购买数量不能大于");

    }


    public void setOnGoods(OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {
        void onGetGood(String goods,int num,String productId,int position);
    }

}
